package controllers

import (
	"covalence/models"
	"covalence/util"
	"github.com/astaxie/beego"
	"strconv"
	"strings"
	"time"
)

type baseController struct {
	beego.Controller
	userid          int64
	username        string
	moduleName      string
	controllerName  string
	actionName      string
	UserTmpFilePath string
	responseMsg     util.ResponseMsg
	dbBasicInfo     models.DBInfo
}

func (this *baseController) Prepare() {
	controllerName, actionName := this.GetControllerAndAction()
	//this.moduleName = "admin" //需要管理员时使用
	this.username = "anonymous" // for any user
	this.responseMsg = *new(util.ResponseMsg)
	this.moduleName = ""
	this.controllerName = strings.ToLower(controllerName[0 : len(controllerName)-10])
	this.actionName = strings.ToLower(actionName)
	//this.auth()
	this.UserTmpFilePath = this.getUserTmpFilePath()
	this.setDBBasicInfo()
}

//是否post提交
func (this *baseController) isPost() bool {
	return this.Ctx.Request.Method == "POST"
}

//获取用户IP地址
func (this *baseController) getClientIp() string {
	s := strings.Split(this.Ctx.Request.RemoteAddr, ":")
	return s[0]
}

func (this *baseController) getTime() time.Time {

	timezone := beego.AppConfig.String("timezone")
	timezonef, _ := strconv.ParseFloat(timezone, 64)

	add := timezonef * float64(time.Hour)
	return time.Now().UTC().Add(time.Duration(add))
	//return time.Now().UTC()
	//return time.Now()
}

func (this *baseController) getUserTmpFilePath() string {
	userTmpFilePath := strings.TrimSpace(beego.AppConfig.String("tmpFilePath")) + "/" + this.username
	return userTmpFilePath
}

func (this *baseController) setDBBasicInfo() {
	this.dbBasicInfo.LigandsCount, _ = strconv.ParseInt(beego.AppConfig.String("LigandsCount"), 10, 64)
	this.dbBasicInfo.TargetsConut, _ = strconv.ParseInt(beego.AppConfig.String("TargetsConut"), 10, 64)
	this.dbBasicInfo.RelationsConut, _ = strconv.ParseInt(beego.AppConfig.String("RelationsConut"), 10, 64)
	this.dbBasicInfo.ReferencesConut, _ = strconv.ParseInt(beego.AppConfig.String("ReferencesConut"), 10, 64)
	this.dbBasicInfo.PositonsConut, _ = strconv.ParseInt(beego.AppConfig.String("PositonsConut"), 10, 64)
	this.dbBasicInfo.IndicationsConut, _ = strconv.ParseInt(beego.AppConfig.String("IndicationsConut"), 10, 64)
	this.dbBasicInfo.SDFile = beego.AppConfig.String("SDFile")
}
